package servlets.site;

import beans.VideoGameFacade;
import entities.JeuVideo;
import helpers.ServletHelper;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * Cette servlet permet de récupérer les données d'un jeu video
 */
@WebServlet(name = "VideoGameServlet", urlPatterns = {"/VideoGameServlet"})
public class VideoGameServlet extends HttpServlet {
    // Gestionnaire
    @EJB
    VideoGameFacade videoGameFacade;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = ServletHelper.getIntegerValue(request.getParameter("id"));
        String forwardTo = "";
        JeuVideo videoGame = null;

        // Si l'id du jeu est valide,
        if (id > 0) {
            videoGame = videoGameFacade.find(id);
            // si le jeu récupéré n'est pas nulle, on l'envoie dans la réponse
            if (videoGame != null) {
                request.setAttribute("jeuVideo", videoGame);
                request.setAttribute("jeuxVideo", videoGameFacade.getSimilars(videoGame, ServletHelper.DVDS_PAR_CATEGORIE));
                request.setAttribute("comments", videoGameFacade.getComments(videoGame));
                System.out.println(videoGameFacade.getAverageNote(videoGame));
                request.setAttribute("average", videoGameFacade.getAverageNote(videoGame));
            }
        }

        if (id == 0 || videoGame == null) {
            forwardTo = "?page=error";
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("home.jsp" + forwardTo);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}